package com.xcity.game.battle.pve;

import java.util.ArrayList;
import java.util.List;

import com.xcity.db.entity.Employee;
import com.xcity.db.entity.Player;
import com.xcity.game.common.Cause;
import com.xcity.game.item.DropGroupTemplate;
import com.xcity.game.item.drop.Drop;
import com.xcity.game.item.drop.impl.ExpDrop;
import com.xcity.game.item.drop.impl.GoldDrop;
import com.xcity.game.item.drop.impl.ItemDrop;
import com.xcity.game.item.drop.listener.ItemDropListener;
import com.xcity.game.npc.NpcTemplate;

import naga.x.App;
import naga.x.game.template.TemplateService;
import naga.x.util.CommonUtils;

public class PVEUtils {

	public static int calcAttack(Player player, List<Employee> employees) {
		int playerAttack = (int) player.getAttackPower();//PlayerUtils.getAttack(player);
		// 员工IQ总和x0.125+技能IQ加成+玩家攻击力
		int iq = 0;
		for (int i = 0, size = employees.size(); i < size; i++) {
//			iq += employees.get(i).getIQ();
		}
		return (int) (iq * 0.125f + playerAttack);
	}
	
	public static int getPlayerInitHP(Player player, List<Employee> employees) {
		int hp = player.getHp();//PlayerUtils.getHP(player);
		for (int i = 0, size = employees.size(); i < size; i++) {
			hp += employees.get(i).getHp();
		}
		return hp;
	}
	
	public static int getEnemyInitHP(Player player, List<Employee> employees, PVEStage stage) {
		return stage.getTemplate().getEnemyHP();
	}
	
	public static void calcPVEStageDrop(Player player, List<Employee> employees, NpcTemplate[] npcs, NpcTemplate boss, List<Drop> resultDropLit, List<Drop> allDrops, float addedRatio, int sweepCount, List<PVEStageDropListener> listenerList, String cause){
		List<Drop> itemDrops = new ArrayList<Drop>(allDrops.size());
		if(resultDropLit == null){
			resultDropLit = new ArrayList<Drop>(allDrops.size());
		}
		int employeeSize = 0;
		for(int i = 0; i < employees.size(); i++){
			if(employees.get(i) != null){
				employeeSize++;
			}
		}
		for(int k = 0; k < sweepCount; k++){
//		// 收入加成技能
			itemDrops.clear();
			PVEStageDropListener listener = new PVEStageDropListener();
			for (int i = 0; i < allDrops.size(); i++) {
				Drop drop = allDrops.get(i);
				// 经验、物品掉落特殊处理
				if (/*employeeSize > 0 && */drop instanceof ExpDrop) {
//					int exp = ((ExpDrop) drop).getExp();
//					int expAdded = 0;
//					if (addedRatio > 0) {
//						expAdded = (int) (exp * addedRatio);
//					}
//					exp = exp / employeeSize;
//					for (int j = 0; j < employees.size(); j++) {
//						Employee e = employees.get(j);
//						if (e != null) {
//							e.addExp(exp, cause);
//							if (expAdded > 0) {
//								e.addExp(expAdded, Cause.EMPLOYEE_SKILL_TRIGGERED);
//							}
//						}
//					}
					drop.drop(Drop.RND, player, cause, listener);
					resultDropLit.add(drop);
				} else if (drop instanceof ItemDrop) {
					itemDrops.add(drop);
				} else {
					drop.drop(Drop.RND, player, cause, listener);
					resultDropLit.add(drop);
					if (addedRatio > 0 && drop instanceof GoldDrop) {
						int gold = (int) (((GoldDrop) drop).getGold() * addedRatio);
						player.addGold(gold, Cause.EMPLOYEE_SKILL_TRIGGERED);
					}
				}
			}
			// 物品最多只掉落8组
			CommonUtils.shuffle(itemDrops);
			for (int i = 0, size = PVEBattle.MAX_DROP_ITEM_SIZE < itemDrops.size() ? PVEBattle.MAX_DROP_ITEM_SIZE : itemDrops.size(); i < size; i++) {
				Drop drop = itemDrops.get(i);
				if (drop.drop(Drop.RND, player, cause, listener)) {
					resultDropLit.add(drop);
				}
			}
			// 处理npc掉落
			for (int i = 0; i < npcs.length; i++) {
				boolean succ = processNpcDrops(npcs[i], player, resultDropLit);
				if (succ) { // 小怪只掉一次
					break;
				}
			}
			processNpcDrops(boss, player, resultDropLit);
			listenerList.add(listener);
		}
	}
	
	public static void calcPVEStageDrop(Player player, List<Employee> employees, PVEStage stage, List<Drop> resultDropLit, List<Drop> allDrops, float addedRatio, int sweepCount, List<PVEStageDropListener> listenerList, String cause){
		calcPVEStageDrop(player, employees, stage.getTemplate().getNpcs(), stage.getTemplate().getBoss(), resultDropLit, allDrops, addedRatio, sweepCount, listenerList, cause);
	}
	
	private static boolean processNpcDrops(NpcTemplate npc, Player player, final List<Drop> result) {
		if (npc == null) {
			return false;
		}
		DropGroupTemplate t = App.getApp().getServiceManager().get(TemplateService.class).get(DropGroupTemplate.class, npc.getDropGropId());
		if (t == null) {
			return false;
		}
		t.getDrop().drop(Drop.RND, player, Cause.PVE_END, new ItemDropListener() {
			@Override
			public void itemDroped(Player player, int itemId, int count) {
				result.add(new ItemDrop(0, itemId, count));
			}
		});
		return true;
	}
	
}
